package com.imcoke.abchat.web.controller;
import com.imcoke.abchat.common.annotation.FormValidation;
import com.imcoke.abchat.domain.*;
import com.imcoke.abchat.domain.json.JsonEmail;
import com.imcoke.abchat.domain.json.JsonLogin;
import com.imcoke.abchat.domain.json.JsonRegister;
import com.imcoke.abchat.domain.json.JsonReset;
import com.imcoke.abchat.web.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;

/**
 * @Author ccoke
 * @Description
 * @Date: 19:39 2017-11-27
 */
@Controller
@RequestMapping("/auth")
public class AuthController {
    private final static Logger logger = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private IUserService mUserService;

    @FormValidation
    @PostMapping("/register")
    @ResponseBody
    public ResponseEntity<ResultBody> postRegister(@RequestBody JsonRegister jsonRegister) throws Exception {
        return ResponseEntity.ok(mUserService.firstGetUser(jsonRegister));
    }

    @GetMapping("/register")
    public String getRegister(@RequestParam(value = "token",required = true) String token, RedirectAttributes attributes) throws Exception {
        StringBuilder message  = new StringBuilder("");
        String mess;
        try {
            if (mUserService.secondGetUser(token, message)){
                mess = "<p>恭喜您已完成邮箱验证！</p>\n" +
                        "            <p><a href=\"/page/login\">>> 开启我的ABChat之旅 <<</a></p>";
            }else {
                mess = "<p>抱歉，注册账号失败。</p>\n" +
                        "            <p>失败原因：<b>" + message.toString() + "</b></p>\n" +
                        "            <p><a href=\"/page/register\">>> 重新注册 <<</a></p>";
            }
        }catch (Exception e){
            mess = "<p>抱歉，注册账号失败。</p>\n" +
                    "            <p>失败原因：<b>验证码已过期</b></p>\n" +
                    "            <p><a href=\"/page/register\">>> 重新注册 <<</a></p>";
        }
        attributes.addFlashAttribute("message",mess);
        return "redirect:/page/message";
    }

    @FormValidation
    @PostMapping("/login")
    @ResponseBody
    public ResponseEntity<ResultBody> postLogin(@RequestBody JsonLogin jsonLogin, Model model){
        return ResponseEntity.ok(mUserService.login(jsonLogin,model));
    }

    @FormValidation
    @PostMapping("/forget")
    @ResponseBody
    public ResponseEntity<ResultBody> postForget(@RequestBody JsonEmail jsonEmail) throws Exception {
        return ResponseEntity.ok(mUserService.firstGetForget(jsonEmail));
    }

    @GetMapping("/forget")
    public String getForget(@RequestParam(value = "token",required = true) String token, RedirectAttributes attributes){
        if(mUserService.secondGetForget(token)){
            attributes.addAttribute("token",token);
            return "redirect:/page/password/reset";
        }else {
            attributes.addAttribute("message","<p>抱歉，重置密码失败。</p>\n" +
                    "            <p>失败原因：<b>验证码已失效</b></p>\n" +
                    "            <p>您可以<a href=\"/page/login\">登录</a>或<a href=\"/page/forget\">找回密码</a></p>");
            return "redirect:/page/message";
        }
    }

    @FormValidation
    @PutMapping("/reset")
    @ResponseBody
    public ResponseEntity<ResultBody> putReset(@RequestBody JsonReset jsonReset, Model model){
        return ResponseEntity.ok(mUserService.resetPassword(jsonReset,model));
    }

    @GetMapping("/email")
    public String getEmail(@RequestParam(value = "token",required = true) String token,Model model) throws IOException {
        try {
            if (mUserService.secondEmail(token,model)){
                model.addAttribute("message","重设邮箱成功！");
                return "html/success";
            }else {
                return "html/error";
            }
        }catch (Exception e){
            return "html/error";
        }
    }

    @GetMapping("/check/username/{username}")
    @ResponseBody
    public ResponseEntity<ResultBody> checkUsername(@PathVariable("username") String username){
        return ResponseEntity.ok(mUserService.checkUsername(username));
    }

    @GetMapping("/check/email")
    @ResponseBody
    public ResponseEntity<ResultBody> checkEmail(@RequestParam("email") String email){
        return ResponseEntity.ok(mUserService.checkEmail(email));
    }

}
